package logic

import (
	"context"
	"course_srv/course"
	"course_srv/internal/svc"
	"course_srv/models/mysql"
	"course_srv/models/redis"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLogic {
	return &CartAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartAddLogic) CartAdd(in *course.CartAddRequest) (*course.CartAddResponse, error) {

	var c mysql.Course
	err := c.CourseShow(int(in.CourseId))
	if err != nil {
		return nil, errors.New("查询课程失败")
	}
	if c.ID == 0 {
		return nil, errors.New("课程不存在")
	}
	m := map[string]interface{}{
		"userId":   in.UserId,
		"courseId": in.CourseId,
		"name":     c.Name,
		"price":    c.Price,
	}
	redis.AddCart(int(in.UserId), int(in.CourseId), m)

	return &course.CartAddResponse{}, nil
}
